﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EasyHome.Models;
using EasyHome.DAL;
using PagedList;

namespace EasyHome.Controllers
{
    public class TablaGenericaController : Controller
    {
        private EasyHomeContext db = new EasyHomeContext();

        #region GET: /TablaGenerica/Index
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.DescripcionSort = String.IsNullOrEmpty(sortOrder) ? "DescripcionSort" : "";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var tablasGenericas = from s in db.TablasGenericas
                                  select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                tablasGenericas = tablasGenericas.Where(s => s.Descripcion.ToUpper().Contains(searchString.ToUpper()));
            }

            switch (sortOrder)
            {
                case "DescripcionSort":
                    tablasGenericas = tablasGenericas.OrderByDescending(s => s.Descripcion);
                    break;
                default:  // Name ascending 
                    tablasGenericas = tablasGenericas.OrderByDescending(s => s.Descripcion);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(tablasGenericas.ToPagedList(pageNumber, pageSize));
        }
        #endregion

        #region GET: /TablaGenerica/Create
        public ActionResult Create()
        {
            return PartialView("Create");
        }
        #endregion

        #region POST: /TablaGenerica/Create
        [HttpPost]
        public ActionResult Create(TablaGenerica tablaGenerica)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        tablaGenerica.id = Guid.NewGuid();
                        db.TablasGenericas.Add(tablaGenerica);
                        db.SaveChanges();
                    }


                    return Json(new { success = true });
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.Message);
                }

            }
            //Something bad happened
            return PartialView("Create", tablaGenerica);
        }
        #endregion

        #region GET: /TablaGenerica/Edit
        // GET: /TablaGenerica/Delete/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaGenerica tablagenerica = db.TablasGenericas.Find(id);
            db.Entry(tablagenerica).Collection(u => u.Datos).Load();

            if (tablagenerica == null)
            {
                return HttpNotFound();
            }

            return View(tablagenerica);
        }
        #endregion

        #region POST: /TablaGenerica/Edit
        // POST: /TablaGenerica/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "id,Descripcion")] TablaGenerica tablagGenerica)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tablagGenerica).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //Something bad happened
            return View(tablagGenerica);
        }
        #endregion

        #region GET: /TablaGenerica/Delete
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaGenerica tablagenerica = db.TablasGenericas.Find(id);
            if (tablagenerica == null)
            {
                return HttpNotFound();
            }

            return PartialView("Delete", tablagenerica);
        }
        #endregion

        #region POST: /TablaGenerica/Delete
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            TablaGenerica tablagenerica = db.TablasGenericas.Find(id);
            db.Entry(tablagenerica).Collection(u => u.Datos).Load();

            if (tablagenerica.Datos.Count == 0) {
                db.TablasGenericas.Remove(tablagenerica);
            }
            db.SaveChanges();


            return Json(new { success = true });
        }
        #endregion

        #region SaveChanges
        static void SaveChanges(TablaGenerica model)
        {
            // Uncommment next line to demonstrate errors in modal
            //throw new Exception("Error test");
        }
        #endregion

        #region Dispose
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion


    }

}

//// GET: /TablaGenerica/Details/5
//public ActionResult Details(Guid? id)
//{
//    if (id == null)
//    {
//        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
//    }
//    TablaGenerica tablagenerica = db.TablasGenericas.Find(id);
//    if (tablagenerica == null)
//    {
//        return HttpNotFound();
//    }
//    return View(tablagenerica);
//}

